<?php
/**
* VIDEO CONFERENCES
* Copyright (c) 2011 
* Author: Dmitriy Tovkach (ragvalod@gmail.com)
* @license		GNU/GPL
**/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');
require_once( JPATH_ROOT.DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php');
require_once (JPATH_COMPONENT.DS.'community.php');
require_once (JPATH_COMPONENT.DS.'lib/API_Config.php');
require_once (JPATH_COMPONENT.DS.'lib/OpenTokSDK.php');


/**

 */
class VideoconferencesViewVideoconference extends JView
{
	function display($tpl = null)
	{
            global $mainframe;
	    
            $document	= &JFactory::getDocument();
	    $user =& JFactory::getUser();
	    $document->setTitle( 'Video Conference' );
	    $id_converence = JRequest::getVar('conference_id',0,'POST');
	    $social  = new VideoSocial();
	    $videoconference = $social->GetVideoconference($id_converence);
            // JS AND CSS
            $document->addScript(JURI::base()."components/com_videoconference/js/jquery.mousewheel.min.js");
            $document->addScript(JURI::base()."components/com_videoconference/js/jquery.iviewer.js");
	    $document->addScript("http://staging.tokbox.com/v0.91/js/TB.min.js");            
	    $document->addStyleSheet( JURI::base()."components/com_videoconference/css/style.css" );
		

		$document->addScript(JURI::base()."components/com_community/assets/script-1.2.js");
		$document->addScript(JURI::base()."components/com_community/assets/window-1.0.js");
		$document->addStyleSheet( JURI::base()."components/com_community/assets/window.css" );
		
            
	    $sessionTokbox = $this->TokBoxInitialisation();
	    if ($videoconference->creator == $user->id)
	    {
		$type_user = "moderator";
		$sessionTokbox['subscribe'] = false;
	    } else
	    {
		$type_user = "guest";
		$sessionTokbox['subscribe'] = true;
	    }
	    $creator_name = $social->GetUserNameAndLocation($videoconference->creator);
	    
	    $this->assignRef('type_user', $type_user);
	    $list_friends = $this->GetListFriends();
	   
	    $this->assignRef('creator_name'   , $creator_name);
	    $this->assignRef('title'   , $videoconference->title);
	    $this->assignRef('description'   , $videoconference->description);
	    $this->assignRef('id_conference'   , $videoconference->id);	    
/*echo "<pre>";
print_r($videoconference);
echo "</pre>"; */
		if (!empty($videoconference->avatar)){
			$image_pass = $videoconference->avatar;
		}else{
			$image_pass = 'components/com_community/templates/blackout/images/vc_event_thumb.png';
		}
		
//	    $this->assignRef('image'   , $videoconference->avatar);
	    $this->assignRef('image'   , $image_pass);
		
	    $this->assignRef('list_friends'   , $list_friends);
	    $this->assignRef('user_data'   , $this->GetUserName());
	    $this->assignRef('user_id'   , $user->id);
	    $this->assignRef('tokbox'   , $sessionTokbox);
	    parent::display($tpl);
        }
	function GetListFriends()
	{
	    $social  = new VideoSocial();
	    return $social->GetListFriends();
	}
	function GetUserName()
	{
	    $social  = new VideoSocial();
	    return $social->GetUserNameAndLocation();
	}
	function TokBoxInitialisation()
	{
	     $apiObj = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);
	     $user =& JFactory::getUser();
	     
             $session = $apiObj->create_session($_SERVER["REMOTE_ADDR"]);

             $obj = array();
	     $session_id_request = JRequest::getVar('SessionId');
	     if ($session_id_request)
	     {
		$obj['sessionID'] = $session_id_request;
	     } else {
		$obj['sessionID'] = $session->getSessionId();
	     }
	     $obj['subscribe'] = false;
	     //$id_converence = JRequest::getVar('conference_id',0,'POST');
	     //$social  = new VideoSocial();
	     //$videoconference = $social->GetVideoconference($id_converence);
	     //if ($videoconference->creator == $user->id)
	     //{
		//$obj['token'] = $apiObj->generate_token('','moderator');
	     //} else
	     //{
		$obj['token'] = $apiObj->generate_token();
	     //}
             
	     return $obj;
	}
}        